﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary;

namespace WebApplication2.EditPages
{
    public partial class Bands : System.Web.UI.Page
    {
        connectionClass conn = new connectionClass();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            GridView1.EditIndex = -1;
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            bool success;
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int bandId = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            TextBox bandName = (TextBox)row.Cells[2].Controls[0];
            TextBox genre = (TextBox)row.Cells[3].Controls[0];
            TextBox site = (TextBox)row.Cells[4].Controls[0];
            try
            {
                conn.getConnection();
                try
                {
                    success = conn.updateBands(bandId, bandName.Text, genre.Text, site.Text);
                    conn.closeConnection();
                    if (success)
                    {
                        Response.Write("<script>alert('De tabel is bijgewerkt');</script>");
                    }
                    GridView1.EditIndex = -1;
                }
                catch (Exception ex)
                {
                    Logs.Log(ex.Message + " " + ex.StackTrace);
                    Response.Write("~/Oops.aspx");
                }
            }
            catch (Exception ex)
            {
                Logs.Log(ex.Message + " " + ex.StackTrace);
                Response.Write("~/Oops.aspx");
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool success;
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int bandId = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            try
            {
                conn.getConnection();
                try{
                    success = conn.deleteBands(bandId);
                    conn.closeConnection();
                    if (success)
                    {
                        Response.Write("<script>alert('Het record is verwijderd');</script>");
                    }
                    GridView1.EditIndex = -1;
                }
                catch (Exception ex)
                {
                    Logs.Log(ex.Message + " " + ex.StackTrace);
                    Response.Write("~/Oops.aspx");
                }
            }
            catch (Exception ex)
            {
                Logs.Log(ex.Message + " " + ex.StackTrace);
                Response.Write("~/Oops.aspx");
            }
        }
    }
}